package com.bonc.common.sso;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.Security;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class EncryptAES {
	public static String DEFAULT_KEY = "asiainfo____2017";
	private static String DEFAULT_CHARSET = "utf-8";
	private static String AES = "AES";
	private static String AES_NOPADDING = "AES/ECB/PKCS5Padding";
	/** 加密工具 */
	private Cipher encryptCipher = null;
	/** 解密工具 */
	private Cipher decryptCipher = null;

	public static EncryptAES getInstance() throws InvalidKeyException, UnsupportedEncodingException,
			NoSuchAlgorithmException, NoSuchPaddingException {
		return new EncryptAES(DEFAULT_KEY, DEFAULT_CHARSET);
	}

	public static EncryptAES getInstance(String key, String charSet) throws InvalidKeyException,
			UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException {
		return new EncryptAES(key, charSet);
	}

	private EncryptAES(String key, String charSet) throws UnsupportedEncodingException, NoSuchAlgorithmException,
			NoSuchPaddingException, InvalidKeyException {
		Security.addProvider(new com.sun.crypto.provider.SunJCE());
		if (key == null || "".equals(key)) {
			key = DEFAULT_KEY;
		}
		if (charSet == null || "".equals(charSet)) {
			charSet = DEFAULT_CHARSET;
		}
		byte[] arrBTmp = key.getBytes(charSet);

		// 创建一个空的length位字节数组（默认值为0）
		byte[] arrB = new byte[16];

		// 将原始字节数组转换为8位
		for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
			arrB[i] = arrBTmp[i];
		}
		// 生成密钥
		Key secretKeySpec = new SecretKeySpec(arrB, AES);

		// 生成Cipher对象,指定其支持的DES算法

		encryptCipher = Cipher.getInstance(AES);
		encryptCipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);

		decryptCipher = Cipher.getInstance(AES);
		decryptCipher.init(Cipher.DECRYPT_MODE, secretKeySpec);

	}

	/**
	 * 对字符串加密
	 * 
	 * @param str
	 * @return
	 * @throws InvalidKeyException
	 * @throws IllegalBlockSizeException
	 * @throws BadPaddingException
	 * @throws UnsupportedEncodingException
	 */
	public byte[] Encrytor(String str, String charSet) throws InvalidKeyException, IllegalBlockSizeException,
			BadPaddingException, UnsupportedEncodingException {
		if (charSet == null || "".equals(charSet)) {
			charSet = DEFAULT_CHARSET;
		}
		byte[] src = str.getBytes(charSet);
		return encryptCipher.doFinal(src);
	}

	/**
	 * 对字符串解密
	 * 
	 * @param buff
	 * @return
	 * @throws InvalidKeyException
	 * @throws IllegalBlockSizeException
	 * @throws BadPaddingException
	 */
	public byte[] Decryptor(byte[] buff) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
		return decryptCipher.doFinal(buff);
	}

	public static String encrypt(String content, String passwd, String charSet) throws NoSuchAlgorithmException,
			NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException,
			UnsupportedEncodingException {
		Cipher aesECB = Cipher.getInstance(AES_NOPADDING);
		SecretKeySpec key = new SecretKeySpec(passwd.getBytes(charSet), AES);
		aesECB.init(Cipher.ENCRYPT_MODE, key);
		byte[] result = aesECB.doFinal(content.getBytes(charSet));
		return (new BASE64Encoder().encode(result)).replaceAll("\\r\\n", "").replaceAll("\\r", "").replaceAll("\\n", "");
	}

	public static String decrypt(String content, String passwd, String charSet) throws NoSuchAlgorithmException,
			NoSuchPaddingException, InvalidKeyException, IOException, IllegalBlockSizeException, BadPaddingException {
		Cipher cipher = Cipher.getInstance(AES_NOPADDING);// 创建密码器
		SecretKeySpec key = new SecretKeySpec(passwd.getBytes(charSet), AES);
		cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
		byte[] result = new BASE64Decoder().decodeBuffer(content);
		return new String(cipher.doFinal(result), charSet); // 解密
	}

	public static String encrypt(String content, String passwd) throws NoSuchAlgorithmException,
			NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException,
			UnsupportedEncodingException {
		Cipher aesECB = Cipher.getInstance(AES_NOPADDING);
		SecretKeySpec key = new SecretKeySpec(passwd.getBytes(DEFAULT_CHARSET), AES);
		aesECB.init(Cipher.ENCRYPT_MODE, key);
		byte[] result = aesECB.doFinal(content.getBytes(DEFAULT_CHARSET));
		return (new BASE64Encoder().encode(result)).replaceAll("\\r\\n", "").replaceAll("\\r", "").replace("\\n", "");
	}

	public String decrypt(String content, String passwd) throws NoSuchAlgorithmException, NoSuchPaddingException,
			InvalidKeyException, IOException, IllegalBlockSizeException, BadPaddingException {
		Cipher cipher = Cipher.getInstance(AES_NOPADDING);// 创建密码器
		SecretKeySpec key = new SecretKeySpec(passwd.getBytes(DEFAULT_CHARSET), AES);
		cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
		byte[] result = new BASE64Decoder().decodeBuffer(content);
		return new String(cipher.doFinal(result), DEFAULT_CHARSET); // 解密
	}

	/**
	 * @param args
	 * @throws NoSuchPaddingException
	 * @throws NoSuchAlgorithmException
	 * @throws BadPaddingException
	 * @throws IllegalBlockSizeException
	 * @throws InvalidKeyException
	 */
	public static void main(String[] args) throws Exception {
		EncryptAES de1 = EncryptAES.getInstance();
		String msg = "郭德纲-搞笑相声全集";
		// String msg = "6bc1bee22e409f96e93d7e117393172a";
		byte[] encontent = de1.Encrytor(msg, "gbk");
		byte[] decontent = de1.Decryptor(encontent);
		System.out.println("明文是:" + msg);
		System.out.println("加密后:" + new String(encontent));
		System.out.println("解密后:" + new String(decontent, "gbk"));
	}
}